package com.boarsoft.flow.core.bean;

import org.mvel2.MVEL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.boarsoft.common.Util;

public class JudgeNode extends WrappedNode {
	private static final Logger log = LoggerFactory.getLogger(JudgeNode.class);

	/** 必需项 */
	protected String yes;
	/** 必需项 */
	protected String no;
	/** handler与expression二者必有其一 */
	protected JudgeHandler handler;
	/** handler与expression二者必有其一 */
	protected String expression;

	public int getType() {
		return BaseNode.TYPE_JUDGE;
	}

	public String getNext(Object data, Throwable throwable) {
		if (handler != null) {
			return handler.judge(data, throwable) ? yes : no;
		}
		if (Util.strIsNotEmpty(expression)) {
			log.warn("Eval MVEL expression: {}", expression);
			return MVEL.evalToBoolean(expression, data) ? yes : no;
		}
		throw new IllegalStateException(id.concat(//
				" is invalid JUDGE node, both handler and expression are not found."));
	}

	public String getYes() {
		return yes;
	}

	public void setYes(String yes) {
		this.yes = yes;
	}

	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	public JudgeHandler getHandler() {
		return handler;
	}

	public void setHandler(JudgeHandler handler) {
		this.handler = handler;
	}

	public String getExpression() {
		return expression;
	}

	public void setExpression(String expression) {
		this.expression = expression;
	}
}
